package com.ficus.road.maintain.third.zh;

import com.alibaba.fastjson.JSONObject;
import com.ficus.road.maintain.core.model.Hit;
import com.ficus.road.maintain.core.model.smartnew.SmartRoadDisease;
import com.ficus.road.maintain.core.model.smartnew.ZHDiseaseLocationIdx;
import com.ficus.road.maintain.core.model.zh.DiseaseLocationRequest;
import com.ficus.road.maintain.core.model.zh.DiseaseLocationZH;
import com.ficus.road.maintain.core.utils.JsonUtils;
import com.ficus.road.maintain.dao.SmartRoadDiseaseDao;
import com.ficus.road.maintain.dao.smartnew.SmartHighwayDao;
import com.ficus.road.maintain.dao.smartnew.ZHDiseaseLocationIdxDao;
import com.ficus.road.maintain.service.HitService;
import com.ficus.road.maintain.util.MyDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xiawen yang
 * @date 2022/7/15 下午4:12
 */
@Component
@Slf4j
public class ZHDiseaseLocation extends BaseZH {

    @Autowired
    private HitService hitService;
    @Autowired
    private SmartHighwayDao smartHighwayDao;
    @Autowired
    private SmartRoadDiseaseDao smartRoadDiseaseDao;
    @Autowired
    private ZHDiseaseLocationIdxDao zhDiseaseLocationIdxDao;
    private Integer connectTimeout;

    @PostConstruct
    void init() {
        arg0 = "ytkDiseaseLocationJudger";
        arg1 = "";
        errMsg = "调用中海接口获取病害位置异常!";
        successMsg = "调用中海接口获取病害位置成功!";
        // 1h
        connectTimeout = 60 * 1000 * 60;
    }

    @Override
    public Integer getConnectTimeout() {
        return this.connectTimeout;
    }

    //        @Scheduled(cron = "0 0 18 * * ?", zone = "GMT+8:00")
//    @Scheduled(cron = "0/5 * * * * ?", zone = "GMT+8:00")
    public void refreshDiseaseLocation() {
        while (true) {
            Date date = MyDateUtil.strParseDate("2022-07-21", "yyyy-MM-dd");
            ZHDiseaseLocationIdx one = zhDiseaseLocationIdxDao.findOne(new Query());
            if (ObjectUtils.isEmpty(one)) {
                one = new ZHDiseaseLocationIdx();
                one.setId("2022-07-21");
                one.setWrongIdxList(new ArrayList<>());
                one.setEmptyMsgIdxList(generateList());
                one.setCreateTime(LocalDateTime.now());
            }
            for (int i = 0; i >= -400; i--) {
                try {
                    List<Integer> emptyMsgIdxList = one.getEmptyMsgIdxList();
                    if (emptyMsgIdxList.get(-i).equals(0)) {
                        continue;
                    }
                    Date param = DateUtils.addDays(date, i);
                    long queryHit = System.currentTimeMillis();
                    List<Hit> correctHitInTime = hitService.findCorrectHitInTime(MyDateUtil.getStartOfDay(param), MyDateUtil.getEndOfDay(param));
                    log.info("i == {}, 查询病害耗时: {}ms", i, System.currentTimeMillis() - queryHit);
                    List<DiseaseLocationRequest.DiseaseLocation> diseaseLocations = correctHitInTime.stream().map(DiseaseLocationRequest.DiseaseLocation::convert2DiseaseLocation).collect(Collectors.toList());
                    DiseaseLocationRequest arg1Object = DiseaseLocationRequest.builder().coordList(diseaseLocations).coordType("2").build();
                    arg1 = JsonUtils.ObjectConvertorString(arg1Object);
                    long l = System.currentTimeMillis();
                    String diseaseLocationStr = getZHData();
                    log.info("同步病害位置耗时: {}s", (System.currentTimeMillis() - l) / 1000);
                    DiseaseLocationZH zh = JSONObject.parseObject(diseaseLocationStr, DiseaseLocationZH.class);
                    if (ObjectUtils.isEmpty(zh)) {
                        continue;
                    }
                    zh.getDiseaseList().stream().flatMap(Collection::stream).map(SmartRoadDisease::convert2SmartRoadDisease)
                            .forEach(smartRoadDiseaseDao::saveOrUpdate);
                    emptyMsgIdxList.set(-i, 0);
                } catch (Exception e) {
                    one.getWrongIdxList().add(i);
                } finally {
                    one.setCurrentIdx(i);
                    one.setUpdateTime(LocalDateTime.now());
                    zhDiseaseLocationIdxDao.saveOrUpdate(one);
                }
            }
        }
    }

    public List<Integer> generateList() {
        List<Integer> res = new ArrayList<>();
        for (int i = 0; i >= -400; i--) {
            res.add(1);
        }
        return res;
    }
}
